﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class PromotionDetail : System.Web.UI.Page
{
    string strUploadImagesPath = System.Configuration.ConfigurationManager.AppSettings["UploadImagesPath"];
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            //string strAction = "";
            if (Request.QueryString["action"] != null)
            {
                if (Request.QueryString["action"].ToString().ToLower() == "add")
                {
                    //strAction = " - เพิ่มข้อมูล";
                    Session["ACTION"] = "add";
                    imgDeal.Visible = false;
                    if (!IsPostBack)
                    {
                        BindDataLocation();
                    }
                }
                else if (Request.QueryString["action"].ToString().ToLower() == "edit")
                {
                    //strAction = " - แก้ไขข้อมูล";
                    Session["ACTION"] = "edit";
                    imgDeal.Visible = false;
                    btnClear.Visible = false;
                    btnClearPeriod.Visible = false;
                }
                else if (Request.QueryString["action"].ToString().ToLower() == "view")
                {
                    //strAction = " - เรียกดูข้อมูล";
                    Session["ACTION"] = "view";
                    btnClear.Visible = false;
                    btnSave.Visible = false;
                    btnClearPeriod.Visible = false;
                    btnStartPromotion.Visible = false;
                    btnEndPromotion.Visible = false;
                }
            }

            if (!IsPostBack)
            {
                Binding.BindTime(ddlStartTime, false, "", "");
                Binding.BindTime(ddlEndTime, false, "", "");
                hdnDealID.Value = Request.QueryString["DealID"];
                //tabPannelDealPeriod.Enabled = false;
                //BindDataLocation();
                BindData();
            }
        }
        catch (Exception ex)
        {
            lblMessage.Text = "<br>" + ex.Message + "<br><br>";
        }
    }

    //protected void btnNext_Click(object sender, EventArgs e)
    //{
    //    tabPannelDealPeriod.Enabled = true;
    //    tabContainerPromotion.ActiveTab = tabContainerPromotion.Tabs[1];
    //    tabPannelDeal.Enabled = false;
    //    if (flDealImage.HasFile)
    //    {
    //        hdnUploadFile.Value = flDealImage.FileName;
    //        hdnUploadFileSize.Value = flDealImage.FileBytes.Length.ToString();
    //    }
    //    else
    //        hdnUploadFile.Value = "";
    //}

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            DataTable dt = new DataTable();
            DBConn oDBConn = new DBConn();
            oDBConn.OpenConnection();

            if (Session["ACTION"].ToString() == "add")
            {
                bool blnValidUpload = false;
                string ShopID = Request.QueryString["ShopID"];
                string imgPath = strUploadImagesPath + ShopID;
                string pathDealImage = "";
                Int32 intLimitImagesSize = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["LimitImagesSizeUploadKb"]);
                if (flDealImage.HasFile)
                {
                    string strExtension = Global.GetFileExtension(flDealImage.FileName).ToLower();
                    if (strExtension.Contains(".gif") || strExtension.Contains(".jpg") || strExtension.Contains(".jpeg") || strExtension.Contains(".png"))
                    {
                        //File Size Upload
                        //Int32 intUploadFileSize = Convert.ToInt32(hdnUploadFileSize.Value);
                        if (flDealImage.FileBytes.Length > 1024 * intLimitImagesSize)
                        {
                            lblMessage.Text = "<br>ขนาดไฟล์เกิน " + intLimitImagesSize + " KB<br>";
                            blnValidUpload = false;
                        }
                        else
                        {
                            flDealImage.SaveAs(Server.MapPath(imgPath + "/" + flDealImage.FileName));
                            pathDealImage = "http://www.poximo.com/images/shopimages/" + ShopID + "/" + flDealImage.FileName;
                            blnValidUpload = true;
                            //hdnUploadFile.Value = "";
                        }
                    }
                    else
                    {
                        lblMessage.Text = "<br>ประเภทไฟล์ไม่ถูกต้อง<br>";
                        blnValidUpload = false;
                    }
                }
                else
                {
                    pathDealImage = "";
                    blnValidUpload = true;
                }
                if (blnValidUpload)
                {
                    SqlCommand oCommand = new SqlCommand("sp_Deal_Deal_INSERT", oDBConn.DBConnection);
                    oCommand.CommandType = CommandType.StoredProcedure;
                    oCommand.Parameters.Add("@Deal_Name", SqlDbType.VarChar).Value = txtPromotionName.Text.Trim();
                    oCommand.Parameters.Add("@Deal_Detail", SqlDbType.VarChar).Value = txtPromotionDetail.Text.Trim();
                    oCommand.Parameters.Add("@Deal_Shop_ID", SqlDbType.BigInt).Value = Request.QueryString["ShopID"];//ddlLocation.SelectedValue.ToString();
                    oCommand.Parameters.Add("@Deal_Code", SqlDbType.VarChar).Value = txtPromotionCode.Text.Trim();
                    oCommand.Parameters.Add("@Deal_Image", SqlDbType.VarChar).Value = pathDealImage;
                    oCommand.Parameters.Add("@Deal_NormalPrice", SqlDbType.VarChar).Value = txtPromotionNormalPrice.Text.Trim();
                    oCommand.Parameters.Add("@Deal_SellPrice", SqlDbType.VarChar).Value = txtPromotionSellPrice.Text.Trim();
                    oCommand.Parameters.Add("@Deal_CreateBy", SqlDbType.VarChar).Value = Session["USER_USERID"].ToString();
                    Int32 LastDealID = Convert.ToInt32(oCommand.ExecuteScalar());
                    //hdnLocationID.Value = LocationID.ToString();

                    oCommand = new SqlCommand("sp_Deal_DealPeriod_INSERT", oDBConn.DBConnection);
                    oCommand.CommandType = CommandType.StoredProcedure;
                    oCommand.Parameters.Add("@DealPeriod_StartPeriod", SqlDbType.Time).Value = ddlStartTime.SelectedValue;
                    oCommand.Parameters.Add("@DealPeriod_EndPeriod", SqlDbType.Time).Value = ddlEndTime.SelectedValue;
                    if (chkSunday.Checked == true)
                    {
                        oCommand.Parameters.Add("@DealPeriod_Sunday", SqlDbType.VarChar).Value = "Sunday";
                    }
                    else
                        oCommand.Parameters.Add("@DealPeriod_Sunday", SqlDbType.VarChar).Value = DBNull.Value;
                    if (chkMonday.Checked == true)
                    {
                        oCommand.Parameters.Add("@DealPeriod_Monday", SqlDbType.VarChar).Value = "Monday";
                    }
                    else
                        oCommand.Parameters.Add("@DealPeriod_Monday", SqlDbType.VarChar).Value = DBNull.Value;
                    if (chkTuesday.Checked == true)
                    {
                        oCommand.Parameters.Add("@DealPeriod_Tuesday", SqlDbType.VarChar).Value = "Tuesday";
                    }
                    else
                        oCommand.Parameters.Add("@DealPeriod_Tuesday", SqlDbType.VarChar).Value = DBNull.Value;
                    if (chkWednesday.Checked == true)
                    {
                        oCommand.Parameters.Add("@DealPeriod_Wednesday", SqlDbType.VarChar).Value = "Wednesday";
                    }
                    else
                        oCommand.Parameters.Add("@DealPeriod_Wednesday", SqlDbType.VarChar).Value = DBNull.Value;
                    if (chkThursday.Checked == true)
                    {
                        oCommand.Parameters.Add("@DealPeriod_Thursday", SqlDbType.VarChar).Value = "Thursday";
                    }
                    else
                        oCommand.Parameters.Add("@DealPeriod_Thursday", SqlDbType.VarChar).Value = DBNull.Value;
                    if (chkFriday.Checked == true)
                    {
                        oCommand.Parameters.Add("@DealPeriod_Friday", SqlDbType.VarChar).Value = "Friday";
                    }
                    else
                        oCommand.Parameters.Add("@DealPeriod_Friday", SqlDbType.VarChar).Value = DBNull.Value;
                    if (chkSaturday.Checked == true)
                    {
                        oCommand.Parameters.Add("@DealPeriod_Saturday", SqlDbType.VarChar).Value = "Saturday";
                    }
                    else
                        oCommand.Parameters.Add("@DealPeriod_Saturday", SqlDbType.VarChar).Value = DBNull.Value;
                    if (txtStartPromotion.Text == "")
                    {
                        oCommand.Parameters.Add("@DealPeriod_StartTime", SqlDbType.VarChar).Value = DBNull.Value;
                    }
                    else
                        oCommand.Parameters.Add("@DealPeriod_StartTime", SqlDbType.VarChar).Value = DateTime.ParseExact(txtStartPromotion.Text, "dd/MM/yyyy", Global.oCulture).ToString("yyyyMMdd", Global.oCulture);
                    if (txtEndPromotion.Text == "")
                    {
                        oCommand.Parameters.Add("@DealPeriod_EndTime", SqlDbType.VarChar).Value = DBNull.Value;
                    }
                    else
                        oCommand.Parameters.Add("@DealPeriod_EndTime", SqlDbType.VarChar).Value = DateTime.ParseExact(txtEndPromotion.Text, "dd/MM/yyyy", Global.oCulture).ToString("yyyyMMdd", Global.oCulture);
                    oCommand.Parameters.Add("@DealPeriod_Deal_ID", SqlDbType.BigInt).Value = LastDealID.ToString();

                    oCommand.ExecuteNonQuery();

                    int i ;
                    for (i = 0; i <= chklLocation.Items.Count - 1; i++)
                    {
                        if (chklLocation.Items[i].Selected == true)
                        {
                            oCommand = new SqlCommand("sp_Deal_DealLocation_INSERT", oDBConn.DBConnection);
                            oCommand.CommandType = CommandType.StoredProcedure;
                            oCommand.Parameters.Add("@DealLocation_Deal_ID ", SqlDbType.BigInt).Value = LastDealID.ToString();
                            oCommand.Parameters.Add("@DealLocation_Location_ID", SqlDbType.BigInt).Value = chklLocation.Items[i].Value;
                            oCommand.ExecuteNonQuery();
                        }
                            
                    }
                    
                    oDBConn.Disconnect();

                    Response.Redirect("PromotionDetail.aspx?ShopID=" + ShopID + "&action=add&DealID=" + LastDealID + "&result=1");
                }
            }
            else
            {
                bool blnValidUpload = false;
                string ShopID = Request.QueryString["ShopID"];
                string imgPath = strUploadImagesPath + ShopID;
                string pathDealImage = "";
                Int32 intLimitImagesSize = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["LimitImagesSizeUploadKb"]);
                if (flDealImage.HasFile)
                {
                    string strExtension = Global.GetFileExtension(flDealImage.FileName).ToLower();
                    if (strExtension.Contains(".gif") || strExtension.Contains(".jpg") || strExtension.Contains(".jpeg") || strExtension.Contains(".png"))
                    {
                        //File Size Upload
                        //Int32 intUploadFileSize = Convert.ToInt32(hdnUploadFileSize.Value);
                        if (flDealImage.FileBytes.Length > 1024 * intLimitImagesSize)
                        {
                            lblMessage.Text = "<br>ขนาดไฟล์เกิน " + intLimitImagesSize + " KB<br>";
                            blnValidUpload = false;
                        }
                        else
                        {
                            flDealImage.SaveAs(Server.MapPath(imgPath + "/" + flDealImage.FileName));
                            pathDealImage = "http://www.poximo.com/images/shopimages/" + ShopID + "/" + flDealImage.FileName;
                            blnValidUpload = true;
                            //hdnUploadFile.Value = "";
                        }
                    }
                    else
                    {
                        lblMessage.Text = "<br>ประเภทไฟล์ไม่ถูกต้อง<br>";
                        blnValidUpload = false;
                    }
                }
                else
                {
                    pathDealImage = hdnDealImage.Value;
                    blnValidUpload = true;
                }
                if (blnValidUpload)
                {
                    //Edit
                    SqlCommand oCommand = new SqlCommand("sp_Deal_Deal_UPDATE", oDBConn.DBConnection);
                    oCommand.CommandType = CommandType.StoredProcedure;
                    oCommand.Parameters.Add("@Deal_ID", SqlDbType.BigInt).Value = hdnDealID.Value;
                    oCommand.Parameters.Add("@Deal_Name", SqlDbType.VarChar).Value = txtPromotionName.Text.Trim();
                    oCommand.Parameters.Add("@Deal_Detail", SqlDbType.VarChar).Value = txtPromotionDetail.Text.Trim();
                    oCommand.Parameters.Add("@Deal_Code", SqlDbType.VarChar).Value = txtPromotionCode.Text.Trim();
                    oCommand.Parameters.Add("@Deal_Image", SqlDbType.VarChar).Value = pathDealImage;
                    oCommand.Parameters.Add("@Deal_NormalPrice", SqlDbType.VarChar).Value = txtPromotionNormalPrice.Text.Trim();
                    oCommand.Parameters.Add("@Deal_SellPrice", SqlDbType.VarChar).Value = txtPromotionSellPrice.Text.Trim();
                    oCommand.ExecuteNonQuery();

                    oCommand = new SqlCommand("sp_Deal_DealPeriod_UPDATE", oDBConn.DBConnection);
                    oCommand.CommandType = CommandType.StoredProcedure;
                    oCommand.Parameters.Add("@DealPeriod_StartPeriod", SqlDbType.Time).Value = ddlStartTime.SelectedValue;
                    oCommand.Parameters.Add("@DealPeriod_EndPeriod", SqlDbType.Time).Value = ddlEndTime.SelectedValue;
                    if (chkSunday.Checked == true)
                    {
                        oCommand.Parameters.Add("@DealPeriod_Sunday", SqlDbType.VarChar).Value = "Sunday";
                    }
                    else
                        oCommand.Parameters.Add("@DealPeriod_Sunday", SqlDbType.VarChar).Value = DBNull.Value;
                    if (chkMonday.Checked == true)
                    {
                        oCommand.Parameters.Add("@DealPeriod_Monday", SqlDbType.VarChar).Value = "Monday";
                    }
                    else
                        oCommand.Parameters.Add("@DealPeriod_Monday", SqlDbType.VarChar).Value = DBNull.Value;
                    if (chkTuesday.Checked == true)
                    {
                        oCommand.Parameters.Add("@DealPeriod_Tuesday", SqlDbType.VarChar).Value = "Tuesday";
                    }
                    else
                        oCommand.Parameters.Add("@DealPeriod_Tuesday", SqlDbType.VarChar).Value = DBNull.Value;
                    if (chkWednesday.Checked == true)
                    {
                        oCommand.Parameters.Add("@DealPeriod_Wednesday", SqlDbType.VarChar).Value = "Wednesday";
                    }
                    else
                        oCommand.Parameters.Add("@DealPeriod_Wednesday", SqlDbType.VarChar).Value = DBNull.Value;
                    if (chkThursday.Checked == true)
                    {
                        oCommand.Parameters.Add("@DealPeriod_Thursday", SqlDbType.VarChar).Value = "Thursday";
                    }
                    else
                        oCommand.Parameters.Add("@DealPeriod_Thursday", SqlDbType.VarChar).Value = DBNull.Value;
                    if (chkFriday.Checked == true)
                    {
                        oCommand.Parameters.Add("@DealPeriod_Friday", SqlDbType.VarChar).Value = "Friday";
                    }
                    else
                        oCommand.Parameters.Add("@DealPeriod_Friday", SqlDbType.VarChar).Value = DBNull.Value;
                    if (chkSaturday.Checked == true)
                    {
                        oCommand.Parameters.Add("@DealPeriod_Saturday", SqlDbType.VarChar).Value = "Saturday";
                    }
                    else
                        oCommand.Parameters.Add("@DealPeriod_Saturday", SqlDbType.VarChar).Value = DBNull.Value;
                    if (txtStartPromotion.Text == "")
                    {
                        oCommand.Parameters.Add("@DealPeriod_StartTime", SqlDbType.VarChar).Value = DBNull.Value;
                    }
                    else
                        oCommand.Parameters.Add("@DealPeriod_StartTime", SqlDbType.VarChar).Value = DateTime.ParseExact(txtStartPromotion.Text, "dd/MM/yyyy", Global.oCulture).ToString("yyyyMMdd", Global.oCulture);
                    if (txtEndPromotion.Text == "")
                    {
                        oCommand.Parameters.Add("@DealPeriod_EndTime", SqlDbType.VarChar).Value = DBNull.Value;
                    }
                    else
                        oCommand.Parameters.Add("@DealPeriod_EndTime", SqlDbType.VarChar).Value = DateTime.ParseExact(txtEndPromotion.Text, "dd/MM/yyyy", Global.oCulture).ToString("yyyyMMdd", Global.oCulture);
                    oCommand.Parameters.Add("@DealPeriod_Deal_ID", SqlDbType.BigInt).Value = hdnDealID.Value;

                    oCommand = new SqlCommand("sp_Deal_DealLocation_DELETE", oDBConn.DBConnection);
                    oCommand.CommandType = CommandType.StoredProcedure;
                    oCommand.Parameters.Add("@DealLocation_Deal_ID ", SqlDbType.BigInt).Value = hdnDealID.Value;

                    oCommand.ExecuteNonQuery();

                    int i;
                    for (i = 0; i <= chklLocation.Items.Count - 1; i++)
                    {
                        if (chklLocation.Items[i].Selected == true)
                        {
                            oCommand = new SqlCommand("sp_Deal_DealLocation_INSERT", oDBConn.DBConnection);
                            oCommand.CommandType = CommandType.StoredProcedure;
                            oCommand.Parameters.Add("@DealLocation_Deal_ID ", SqlDbType.BigInt).Value = hdnDealID.Value;
                            oCommand.Parameters.Add("@DealLocation_Location_ID", SqlDbType.BigInt).Value = chklLocation.Items[i].Value;
                            oCommand.ExecuteNonQuery();
                        }

                    }

                    oDBConn.Disconnect();

                    Response.Redirect("PromotionDetail.aspx?ShopID=" + ShopID + "&action=edit&DealID=" + hdnDealID.Value + "&result=1");
                }
            }
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message;
        }
    }

    private void BindDataLocation()
    {
        DataTable dt = new DataTable();
        DBConn oDBConn = new DBConn();
        oDBConn.OpenConnection();

        SqlCommand oCommand = new SqlCommand("sp_Deal_Location_SELECTBYLOCATIONSHOPID", oDBConn.DBConnection);
        oCommand.CommandType = CommandType.StoredProcedure;
        oCommand.Parameters.Add("@Location_Shop_ID", SqlDbType.BigInt).Value = Request.QueryString["ShopID"];

        SqlDataAdapter oDataAdapter = new SqlDataAdapter(oCommand);
        oDataAdapter.Fill(dt);
        oDataAdapter = null;
        oDBConn.Disconnect();

        if (dt.Rows.Count > 0)
        {
            DataTable dtLocation = dt.DefaultView.ToTable("dtLocation");

            //ddlLocation.Items.Clear();
            //ddlLocation.DataSource = dtLocation;
            //ddlLocation.DataTextField = "Location_Name";
            //ddlLocation.DataValueField = "Location_ID";
            //ddlLocation.DataBind();

            chklLocation.Items.Clear();
            chklLocation.DataSource = dtLocation;
            chklLocation.DataTextField = "Location_Name";
            chklLocation.DataValueField = "Location_ID";
            chklLocation.DataBind();
        }
        else
        {
            lblMessage.Text = "<br>กรุณากรอกข้อมูลสาขาที่จะทำโปรโมชั่น<br>";
        }
    }

    private void BindDealLocation()
    {
        DataTable dt = new DataTable();
        DBConn oDBConn = new DBConn();
        oDBConn.OpenConnection();

        SqlCommand oCommand = new SqlCommand("sp_Deal_DealLocation_SELECTBYDEALID", oDBConn.DBConnection);
        oCommand.CommandType = CommandType.StoredProcedure;
        oCommand.Parameters.Add("@Deal_ID", SqlDbType.BigInt).Value = hdnDealID.Value;

        SqlDataAdapter oDataAdapter = new SqlDataAdapter(oCommand);
        oDataAdapter.Fill(dt);
        oDataAdapter = null;
        oDBConn.Disconnect();

        if (dt.Rows.Count > 0)
        {
            DataTable dtDealLocation = dt.DefaultView.ToTable("dtDealLocation");

            chklLocation.Items.Clear();
            chklLocation.DataSource = dtDealLocation;
            chklLocation.DataTextField = "Location_Name";
            chklLocation.DataValueField = "Location_ID";
            chklLocation.DataBind();
            int i;
            for (i = 0; i <= dtDealLocation.Rows.Count - 1; i++)
            {
                chklLocation.Items[i].Selected = true;
            }
        }
        else
        {
            lblMessage.Text = "<br>กรุณากรอกข้อมูลสาขาที่จะทำโปรโมชั่น<br>";
        }
    }

    private void BindLocationEdit()
    {
        DataTable dtLocationDeal = new DataTable();
        DataTable dtLocationShop = new DataTable();
        DataSet ds = new DataSet();
        DBConn oDBConn = new DBConn();
        oDBConn.OpenConnection();

        SqlCommand oCommand = new SqlCommand("sp_Deal_DealLocation_SELECTBYDEALID", oDBConn.DBConnection);
        oCommand.CommandType = CommandType.StoredProcedure;
        oCommand.Parameters.Add("@Deal_ID", SqlDbType.BigInt).Value = hdnDealID.Value;
        SqlDataAdapter oDataAdapter = new SqlDataAdapter(oCommand);
        oDataAdapter.Fill(dtLocationDeal);
        ds.Tables.Add(dtLocationDeal);
        oDataAdapter = null;

        oCommand = new SqlCommand("sp_Deal_Location_SELECTBYLOCATIONSHOPID", oDBConn.DBConnection);
        oCommand.CommandType = CommandType.StoredProcedure;
        oCommand.Parameters.Add("@Location_Shop_ID", SqlDbType.BigInt).Value = Request.QueryString["ShopID"];

        oDataAdapter = new SqlDataAdapter(oCommand);
        oDataAdapter.Fill(dtLocationShop);
        oDataAdapter = null;
        oDBConn.Disconnect();

        if (dtLocationShop.Rows.Count > 0)
        {
            DataTable dtDealLocation = dtLocationShop.DefaultView.ToTable("dtDealLocation");

            chklLocation.Items.Clear();
            chklLocation.DataSource = dtDealLocation;
            chklLocation.DataTextField = "Location_Name";
            chklLocation.DataValueField = "Location_ID";
            chklLocation.DataBind();

            int i, j;
            string ShopDeal,LocationDeal = string.Empty;
            for (i = 0; i <= dtLocationDeal.Rows.Count - 1; i++)
            {
                LocationDeal = ds.Tables[0].Rows[i]["Location_ID"].ToString(); 

                for (j = 0; j <= dtDealLocation.Rows.Count - 1; j++)
                {
                    ShopDeal = chklLocation.Items[j].Value;
                    if (LocationDeal == ShopDeal)
                    {
                           chklLocation.Items[j].Selected = true;
                    }

                }
            }
        }
        else
        {
            lblMessage.Text = "<br>กรุณากรอกข้อมูลสาขาที่จะทำโปรโมชั่น<br>";
        }
    }

    private void BindData()
    {
        if (Request.QueryString["result"] != null && Request.QueryString["result"].ToString() == "1")
        {
            if (Session["ACTION"].ToString() == "edit")
                lblMessage.Text = "<br>แก้ไขข้อมูลเรียบร้อยแล้ว<br>";
            else
                lblMessage.Text = "<br>เพิ่มข้อมูลเรียบร้อยแล้ว<br>";
        }
        if (Session["ACTION"].ToString() == "edit" || Session["ACTION"].ToString() == "view")
        {
            DataTable dt = new DataTable();
            DBConn oDBConn = new DBConn();
            oDBConn.OpenConnection();

            SqlCommand oCommand = new SqlCommand("sp_Deal_Deal_SELECTBYDEALID", oDBConn.DBConnection);
            oCommand.CommandType = CommandType.StoredProcedure;
            oCommand.Parameters.Add("@Deal_ID", SqlDbType.BigInt).Value = hdnDealID.Value;

            SqlDataAdapter oDataAdapter = new SqlDataAdapter(oCommand);
            oDataAdapter.Fill(dt);
            oDataAdapter = null;
            oDBConn.Disconnect();

            if (dt.Rows.Count > 0)
            {
                if (Session["ACTION"].ToString() == "edit")
                {
                    //ddlLocation.Visible = false;
                    //lblLocation.Text = dt.Rows[0]["Location_Name"].ToString();
                    //BindDealLocation();
                    BindLocationEdit();
                    txtPromotionName.Text = dt.Rows[0]["Deal_Name"].ToString();
                    txtPromotionDetail.Text = dt.Rows[0]["Deal_Detail"].ToString();
                    txtPromotionCode.Text = dt.Rows[0]["Deal_Code"].ToString();
                    hdnDealImage.Value = dt.Rows[0]["Deal_Image"].ToString();
                    txtPromotionNormalPrice.Text = dt.Rows[0]["Deal_NormalPrice"].ToString();
                    txtPromotionSellPrice.Text = dt.Rows[0]["Deal_SellPrice"].ToString();

                    txtStartPromotion.Text = dt.Rows[0]["DealPeriod_StartTime"].ToString();
                    txtEndPromotion.Text = dt.Rows[0]["DealPeriod_EndTime"].ToString();
                    ddlStartTime.SelectedValue = dt.Rows[0]["DealPeriod_StartPeriod"].ToString();
                    ddlEndTime.SelectedValue = dt.Rows[0]["DealPeriod_EndPeriod"].ToString();
                    if (dt.Rows[0]["DealPeriod_Sunday"].ToString() != "")
                    {
                        chkSunday.Checked = true;
                    }
                    else
                        chkSunday.Checked = false;
                    if (dt.Rows[0]["DealPeriod_Monday"].ToString() != "")
                    {
                        chkMonday.Checked = true;
                    }
                    else
                        chkMonday.Checked = false;
                    if (dt.Rows[0]["DealPeriod_Tuesday"].ToString() != "")
                    {
                        chkTuesday.Checked = true;
                    }
                    else
                        chkTuesday.Checked = false;
                    if (dt.Rows[0]["DealPeriod_Sunday"].ToString() != "")
                    {
                        chkSunday.Checked = true;
                    }
                    else
                        chkSunday.Checked = false;
                    if (dt.Rows[0]["DealPeriod_Wednesday"].ToString() != "")
                    {
                        chkWednesday.Checked = true;
                    }
                    else
                        chkWednesday.Checked = false;
                    if (dt.Rows[0]["DealPeriod_Thursday"].ToString() != "")
                    {
                        chkThursday.Checked = true;
                    }
                    else
                        chkThursday.Checked = false;
                    if (dt.Rows[0]["DealPeriod_Friday"].ToString() != "")
                    {
                        chkFriday.Checked = true;
                    }
                    else
                        chkFriday.Checked = false;
                    if (dt.Rows[0]["DealPeriod_Saturday"].ToString() != "")
                    {
                        chkSaturday.Checked = true;
                    }
                    else
                        chkSaturday.Checked = false;
                    if (chkSunday.Checked == true && chkMonday.Checked == true && chkTuesday.Checked == true && chkWednesday.Checked == true && chkThursday.Checked == true && chkFriday.Checked == true && chkSaturday.Checked == true)
                    {
                        chkEveryday.Checked = true;

                        chkSunday.Enabled = false;
                        chkMonday.Enabled = false;
                        chkTuesday.Enabled = false;
                        chkWednesday.Enabled = false;
                        chkThursday.Enabled = false;
                        chkFriday.Enabled = false;
                        chkSaturday.Enabled = false;
                    }
                    else
                    {
                        chkEveryday.Checked = false;

                        chkSunday.Enabled = true;
                        chkMonday.Enabled = true;
                        chkTuesday.Enabled = true;
                        chkWednesday.Enabled = true;
                        chkThursday.Enabled = true;
                        chkFriday.Enabled = true;
                        chkSaturday.Enabled = true;
                    }
                }
                else
                {
                    //ddlLocation.Visible = false;
                    chklLocation.Enabled = false;
                    txtPromotionName.Visible = false;
                    txtPromotionDetail.Visible = false;
                    txtPromotionCode.Visible = false;
                    flDealImage.Visible = false;
                    txtPromotionNormalPrice.Visible = false;
                    txtPromotionSellPrice.Visible = false;

                    txtStartPromotion.Visible = false;
                    txtEndPromotion.Visible = false;
                    ddlStartTime.Visible = false;
                    ddlEndTime.Visible = false;
                    chkEveryday.Enabled = false;
                    chkSunday.Enabled = false;
                    chkMonday.Enabled = false;
                    chkTuesday.Enabled = false;
                    chkWednesday.Enabled = false;
                    chkThursday.Enabled = false;
                    chkFriday.Enabled = false;
                    chkSaturday.Enabled = false;

                    //lblLocation.Text = dt.Rows[0]["Location_Name"].ToString();
                    BindDealLocation();
                    lblPromotionName.Text = dt.Rows[0]["Deal_Name"].ToString();
                    lblPromotionDetail.Text = dt.Rows[0]["Deal_Detail"].ToString();
                    lblPromotionCode.Text = dt.Rows[0]["Deal_Code"].ToString();
                    imgDeal.ImageUrl = dt.Rows[0]["Deal_Image"].ToString();
                    lblPromotionNormalPrice.Text = dt.Rows[0]["Deal_NormalPrice"].ToString();
                    lblPromotionSellPrice.Text = dt.Rows[0]["Deal_SellPrice"].ToString();

                    lblStartPromotion.Text = dt.Rows[0]["DealPeriod_StartTime"].ToString();
                    lblEndPromotion.Text = dt.Rows[0]["DealPeriod_EndTime"].ToString();
                    lblStartTime.Text = dt.Rows[0]["DealPeriod_StartPeriod"].ToString();
                    lblEndTime.Text = dt.Rows[0]["DealPeriod_EndPeriod"].ToString();
                    if (dt.Rows[0]["DealPeriod_Sunday"].ToString() != "")
                    {
                        chkSunday.Checked = true;
                    }
                    else
                        chkSunday.Checked = false;
                    if (dt.Rows[0]["DealPeriod_Monday"].ToString() != "")
                    {
                        chkMonday.Checked = true;
                    }
                    else
                        chkMonday.Checked = false;
                    if (dt.Rows[0]["DealPeriod_Tuesday"].ToString() != "")
                    {
                        chkTuesday.Checked = true;
                    }
                    else
                        chkTuesday.Checked = false;
                    if (dt.Rows[0]["DealPeriod_Sunday"].ToString() != "")
                    {
                        chkSunday.Checked = true;
                    }
                    else
                        chkSunday.Checked = false;
                    if (dt.Rows[0]["DealPeriod_Wednesday"].ToString() != "")
                    {
                        chkWednesday.Checked = true;
                    }
                    else
                        chkWednesday.Checked = false;
                    if (dt.Rows[0]["DealPeriod_Thursday"].ToString() != "")
                    {
                        chkThursday.Checked = true;
                    }
                    else
                        chkThursday.Checked = false;
                    if (dt.Rows[0]["DealPeriod_Friday"].ToString() != "")
                    {
                        chkFriday.Checked = true;
                    }
                    else
                        chkFriday.Checked = false;
                    if (dt.Rows[0]["DealPeriod_Saturday"].ToString() != "")
                    {
                        chkSaturday.Checked = true;
                    }
                    else
                        chkSaturday.Checked = false;
                    if (chkSunday.Checked == true && chkMonday.Checked == true && chkTuesday.Checked == true && chkWednesday.Checked == true && chkThursday.Checked == true && chkFriday.Checked == true && chkSaturday.Checked == true)
                    {
                        chkEveryday.Checked = true;
                    }
                    else
                        chkEveryday.Checked = false;
                }

            }
        }
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("Promotion.aspx");
    }

    private void BindCheckTime()
    {
        if (chkEveryday.Checked == true)
        {
            chkSunday.Checked = true;
            chkSunday.Enabled = false;
            chkMonday.Checked = true;
            chkMonday.Enabled = false;
            chkTuesday.Checked = true;
            chkTuesday.Enabled = false;
            chkWednesday.Checked = true;
            chkWednesday.Enabled = false;
            chkThursday.Checked = true;
            chkThursday.Enabled = false;
            chkFriday.Checked = true;
            chkFriday.Enabled = false;
            chkSaturday.Checked = true;
            chkSaturday.Enabled = false;
        }
        else
        {
            chkSunday.Checked = false;
            chkSunday.Enabled = true;
            chkMonday.Checked = false;
            chkMonday.Enabled = true;
            chkTuesday.Checked = false;
            chkTuesday.Enabled = true;
            chkWednesday.Checked = false;
            chkWednesday.Enabled = true;
            chkThursday.Checked = false;
            chkThursday.Enabled = true;
            chkFriday.Checked = false;
            chkFriday.Enabled = true;
            chkSaturday.Checked = false;
            chkSaturday.Enabled = true;
        }
    }

    protected void chkEveryday_CheckedChanged(object sender, EventArgs e)
    {
        BindCheckTime();
    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        txtPromotionName.Text = "";
        txtPromotionDetail.Text = "";
        txtPromotionCode.Text = "";
        txtPromotionNormalPrice.Text = "";
        txtPromotionSellPrice.Text = "";
    }

    protected void btnClearPeriod_Click(object sender, EventArgs e)
    {
        ddlStartTime.SelectedValue = "00:00";
        ddlEndTime.SelectedValue = "00:00";
        txtStartPromotion.Text = "";
        txtEndPromotion.Text = "";
        chkEveryday.Checked = false;
        chkSunday.Checked = false;
        chkSunday.Enabled = true;
        chkMonday.Checked = false;
        chkMonday.Enabled = true;
        chkTuesday.Checked = false;
        chkTuesday.Enabled = true;
        chkWednesday.Checked = false;
        chkWednesday.Enabled = true;
        chkThursday.Checked = false;
        chkThursday.Enabled = true;
        chkFriday.Checked = false;
        chkFriday.Enabled = true;
        chkSaturday.Checked = false;
        chkSaturday.Enabled = true;
    }
}

